;// 会計年度
(function($, undefined) {
  "use strict";
  $.fn.balanceCalender = function(option) {
    var option = $.extend({
      startMonth : 0,
      format : 'yyyy-mm-dd',
      formatM : 'YYYY-MM-DD',
      container : '.control-group',
      prevDate : '.icon-arrow-left',
      nextDate : '.icon-arrow-right',
      modes : 'ul li',
      modeActive : 'active',
      datepicker : {
        startView : 1,
        minViewMode : 1,
        autoclose : true,
        forceParse : true,
        multidateSeparator : ' to '
      },
    }, option);
    function getContainer(e) {
      return e.closest(option.container);
    }
    function calculateDate(v, i, mode, move, format) {
      var d = v ? moment(v, format) : moment();
      if (mode == 3) { // custom
        return d;
      }
      var m = d.month();
      if (mode == 2) { // year
        d.add('years', move);
        if (m < option.startMonth) {
          d.add('years', -1).month(option.startMonth).dates(1);
        };
        return (i == 0) ? d.dates(1) : d.add('months', 12).dates(0);
      }
      if (mode == 1) { // quarter
        d.add('months', move * 3);
        var dm = option.startMonth % 3 - m % 3
        if (dm != 0) {
          d.add('months', -Math.abs(dm)).dates(1);
        };
        return (i == 0) ? d.dates(1) : d.add('months', 3).dates(0);
      }
      d.add('months', move);
      return (i == 0) ? d.dates(1) : d.add('months', 1).dates(0);
    }
    function updateDate(c, move) {
      var dataBC = c.data('dataBC');
      var input = dataBC.input;
      var mode = dataBC.container.find('.' + option.modeActive).index();
      if (mode == 3) {
        input.on('focus', function() {
          input.datepicker('hide');
        });
        return;
      }
      var d = input.val().split(option.datepicker.multidateSeparator);
      var newDate = [];
      newDate[0] = calculateDate(d[0], 0, mode, move, option.formatM).format(option.formatM);
      newDate[1] = calculateDate(d[0], 1, mode, move, option.formatM).format(option.formatM);
      input.datepicker('setDates', newDate).datepicker('fill');
    }
    function initialize(e) {
      var $c = getContainer(e);
      var dataBC = {
        container : $c,
        input : e,
        mode : 0
      };
      $c.data('dataBC', dataBC);
      e.datepicker($.extend({
        format : option.format
      }, option.datepicker));
      var $modes = $c.find(option.modes);
      $modes.click(function() {
        $(this).addClass(option.modeActive).siblings().removeClass(option.modeActive);
        var $c = getContainer($(this));
        updateDate($c, 0);
      });
      $c.find(option.prevDate).click(function() {
        var $c = getContainer($(this));
        updateDate($c, -1);
      });
      $c.find(option.nextDate).click(function() {
        var $c = getContainer($(this));
        updateDate($c, 1);
      });
    };
    this.each(function() {
      initialize($(this));
    });
    $.fn.publicMethod = function(options) {};
    return this;
  };
})(jQuery);
